package lzupdater.gui;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import lzupdater.com.CommunicationHub;
import lzupdater.com.HubComponent;
import lzupdater.gui.elements.LocalizedJMenu;
import lzupdater.gui.elements.LocalizedJMenuItem;

//LZRosterUpdater, a java client to update LZRoster
//Copyright (C) 2008 Fraskor from Krasus (e-mail: fraskor@gmail.com)
//
//This file is part of LZRosterUpdater.
//
//LZRosterUpdater is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.

public class MenuBar extends HubComponent {

//	private final static LocalizedString
	private JMenuBar bar;
	private MainGui mainGui;
	private PreferencesPanel preferencesPanel;
	private AboutFrame aboutFrame;
	private DebugPanel debugFrame;
	
	public MenuBar(CommunicationHub hub) {
		super(hub);
		bar = new JMenuBar();
	}
	
	
	@Override
	public void initializeCommunication() {
		mainGui = hub.getComponent(MainGui.class);
		preferencesPanel = hub.getComponent(PreferencesPanel.class);
		aboutFrame = hub.getComponent(AboutFrame.class);
		debugFrame = hub.getComponent(DebugPanel.class);
	}

	@Override
	public void initialize() {
		initializeGui();
	}

	public JMenuBar getGui() {
		return bar;
	}
	
	private void initializeGui() {
		JMenu menu;
		JMenuItem menuItem;
		
		menu = new LocalizedJMenu("File");
		menu.setMnemonic(KeyEvent.VK_F);

		menuItem = new LocalizedJMenuItem("About");
		menu.add(menuItem);
		
		menuItem.setAction(new DisplayAction(aboutFrame.getGui()));
		
		menu.addSeparator();
		
		menuItem = new LocalizedJMenuItem("Exit", KeyEvent.VK_Q);

		menuItem.addActionListener(new AbstractAction() {
			/**
			 * 
			 */
			private static final long serialVersionUID = 9125386764628831440L;

			public void actionPerformed(ActionEvent e) {
				mainGui.windowClosing(null);
			}
		});
		menu.add(menuItem);
	
		
		bar.add(menu);
		
		menu = new LocalizedJMenu("Windows");
		
		menu.add(new WindowsMenuItem("Preferences", preferencesPanel.getGui()));
		menu.add(new WindowsMenuItem("Debug", debugFrame.getGui()));
		
		
		bar.add(menu);		
	}
	
	public class DisplayAction extends AbstractAction {
		
		/**
		 * 
		 */
		private static final long serialVersionUID = -760999441203908409L;
		private JFrame frame;
		
		public DisplayAction(JFrame frame) {
			this.frame = frame;
		}
		
		public void actionPerformed(ActionEvent arg0) {
			frame.setVisible(true);
		}
		
	}


}
